package com.tcs.reptile;

import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Lists;
import com.tcs.cache.ICommonCacheTemplate;
import com.tcs.cache.redis.RedisCacheTemplate;
import com.tcs.check.net.INetChecked;
import com.tcs.check.net.NetChecked;
import com.tcs.connection.IProxyPoolConnection;
import com.tcs.connection.ProxyPoolConnection;
import com.tcs.model.ipdata.IpDataBean;
import com.tcs.model.porxypool.proxyPoolBean;
import com.tcs.model.porxypool.RequestParamsBean;
import com.tcs.server.ipdata.IpDataService;
import com.tcs.server.ipdata.impl.IpDataServiceImpl;
import com.tcs.util.constant.SystemConstants;
import com.tcs.util.spring.SpringConfigTool;

public abstract class AbstractReptileTask implements IReptileTask {
	
	private final Logger logger = LoggerFactory.getLogger(AbstractReptileTask.class);
	
	private proxyPoolBean proxyPoolBean;
	
	private RequestParamsBean requestParamsBean;
	
	@SuppressWarnings("unused")
	private CountDownLatch countDownLatch;		// 预留
	
	private IProxyPoolConnection iProxyPoolConnection;
	
	private ICommonCacheTemplate iCommonCacheTemplate;
	
	private IpDataService ipDataService;
	
	private Pattern pattern;
	
	private INetChecked iNetChecked;
	
	public AbstractReptileTask(){};
	
	public AbstractReptileTask(proxyPoolBean proxyPoolBean) {
		this.proxyPoolBean = proxyPoolBean;
	}
	
	public AbstractReptileTask(proxyPoolBean proxyPoolBean, RequestParamsBean requestParamsBean , CountDownLatch countDownLatch) {
		this.proxyPoolBean = proxyPoolBean;
		this.requestParamsBean = requestParamsBean;
		this.countDownLatch = countDownLatch;
		this.iNetChecked = SpringConfigTool.getBean(NetChecked.class);
		this.ipDataService = SpringConfigTool.getBean(IpDataServiceImpl.class);
		this.iProxyPoolConnection = SpringConfigTool.getBean(ProxyPoolConnection.class);
		this.iCommonCacheTemplate = SpringConfigTool.getBean(RedisCacheTemplate.class);
		this.pattern = Pattern.compile(this.requestParamsBean.getRegular());
	}
	
	@Override
	public void clean() {
		this.requestParamsBean.setTmepUrl(null);
	}
	
	@Override
	public void go() {
		String [] array = null;
		IpDataBean ipDataBean = null;
		try {
			List<IpDataBean> ipDataList = Lists.newArrayListWithExpectedSize(30);
			Matcher matcher = pattern.matcher(
				iProxyPoolConnection.connection(
					this.requestParamsBean.getTmepUrl().toString().trim(), 
					this.requestParamsBean.getTimeOut(), 
					this.requestParamsBean.getRetriesNum()).text()
				);
			this.clean();
			while (matcher.find()) {
				array = matcher.group().split(" ");
				logger.debug(" ip : {} , prot : {} , proxyPoolBean ", array[0], array[1], proxyPoolBean);
				ipDataBean = new IpDataBean(array[0], NumberUtils.toInt(array[1]));
				if (!iCommonCacheTemplate.exists(ipDataBean.toString())) {
					if (iNetChecked.checkThrough(ipDataBean)) {
						logger.info(" 验证通过  : {} ", ipDataBean.toString());
						// 减少入库连接次数
						ipDataList.add(new IpDataBean(array[0], NumberUtils.toInt(array[1])));
						logger.debug("setex : {} bean 验证通过" , ipDataBean);
						iCommonCacheTemplate.setex(ipDataBean.toString(), SystemConstants.ProxyPool.cacheTime, "");
					} else {
						// 进入cache中,不再花时间网络验证
						logger.debug("setex : {} bean " , ipDataBean);
						iCommonCacheTemplate.setex(ipDataBean.toString(), SystemConstants.ProxyPool.cacheTime, "");
					}
				}
			}
			if (!ipDataList.isEmpty()) {
				ipDataService.insertList(ipDataList);
			}
		} catch (Exception e) {
			logger.error("" , e);
		}
	}
}